<?php

namespace app\modules\Cruise\models;

use Yii;

/**
 * This is the model class for table "room".
 *
 * @property integer $id
 * @property string $cruise_id
 * @property string $name
 * @property string $code
 * @property integer $windows
 * @property integer $area
 * @property string $num
 * @property string $ceil
 * @property string $desc
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 * @property integer $balcony
 * @property integer $balcony_area
 */
class Room extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'room';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cruise_id'], 'required'],
            [['windows', 'status', 'user_id', 'balcony', 'type', 'min', 'sort'], 'integer'],
            [['created_at', 'updated_at', 'ceil'], 'safe'],
            [['cruise_id'], 'string', 'max' => 11],
            [['name'], 'string', 'max' => 128],
            [['code','area',  'num', 'balcony_area'], 'string', 'max' => 16],
           // [['ceil'], 'string', 'max' => 64],
            [['desc'], 'string', 'max' => 512],
        ];
    }


    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }

        if(is_array($this->ceil)){
            $this->ceil = json_encode($this->ceil);
        }
        //var_dump($this->ceil);
        //exit;
        return parent::beforeSave($insert);
    }//}}}


    //  获取对应游轮   
    public function getCruise(){
        return $this->hasOne(Cruise::className(), ['id' => 'cruise_id']);
    }

    // 获取对应图片
    public function getImage(){
        return  $this->hasOne(Image::className(),['product_id'=>'id'])->onCondition(['image.type'=>2,'image.status'=>1]);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.cruise', 'ID'),
            'cruise_id' => Yii::t('app.cruise', 'Cruise ID'),
            'name' => Yii::t('app.cruise', 'Name'),
            'code' => Yii::t('app.cruise', 'Code'),
            'windows' => Yii::t('app.cruise', 'Windows'),
            'area' => Yii::t('app.cruise', 'Area'),
            'num' => Yii::t('app.cruise', 'Num'),
            'ceil' => Yii::t('app.cruise', 'Ceil'),
            'desc' => Yii::t('app.cruise', 'Desc'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'balcony' => Yii::t('app.cruise', 'Balcony'),
            'balcony_area' => Yii::t('app.cruise', 'Balcony Area'),
            'type' => Yii::t('app.cruise', 'Room Type'),
            'min' => Yii::t('app.cruise', 'Min'),
            'sort' => Yii::t('app.cruise', 'Sort'),
        ];
    }
}
